package com.jxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxx.anno.CacheFind;
import com.jxx.mapper.AddressMapper;
import com.jxx.mapper.AdminInfoMapper;
import com.jxx.mapper.AdminMapper;
import com.jxx.mapper.AdminSecurityMapper;
import com.jxx.pojo.Admin;
import com.jxx.pojo.AdminInfo;
import com.jxx.pojo.AdminSecurity;
import com.jxx.service.AdminService;
import com.jxx.vo.SysResult;
import com.jxx.vo.UpdatePassword;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminInfoMapper adminInfoMapper;
    @Autowired
    private AdminSecurityMapper adminSecurityMapper;
    @Autowired
    private AddressMapper addressMapper;

    /**
     * @author Zcc
     * @desc 判断密码
     * @param   adminAccount,adminPassword
     * @return SysResult
     * @date 2020年11月21日20:33:54
     */
    @Override
    @CacheFind(preKey = "admin")
    public SysResult getPassword(String adminAccount,String adminPassword) {
        if(StringUtils.isEmpty(adminAccount))
            return SysResult.fail();
        if(StringUtils.isEmpty(adminPassword))
            return SysResult.fail();
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_account", adminAccount);
        Admin admin = adminMapper.selectOne(queryWrapper);
        if (admin==null)return SysResult.fail();
        SimpleHash sh = new SimpleHash(
                "MD5",
                adminPassword,
                admin.getSalt(),
                10);
        if (!admin.getAdminPassword().equals(sh.toHex())){
            return SysResult.fail("密码错误");
        }else {
            return SysResult.success();
        }
    }

    /**
     * @author Zcc
     * @desc 管理员信息
     * @param   adminId
     * @return AdminInfo
     * @date 2020年11月21日20:33:54
     */
    @Override
    @CacheFind(preKey = "adminInfo")
    public AdminInfo showInfo(Integer adminId) {
        return adminInfoMapper.selectById(adminId);
    }

    /**
     * @author Zcc
     * @desc 修改管理员信息
     * @param   adminInfo
     * @return SysResult
     * @date 2020年11月21日20:33:54
     */
    @Override
    public SysResult updateInfo(AdminInfo adminInfo) {
        adminInfoMapper.updateById(adminInfo);
        return SysResult.success();
    }

    /**
     * @author Zcc
     * @desc 展示管理员密保问题
     * @param   adminId
     * @return List<AdminSecurity>
     * @date 2020年11月21日20:33:54
     */
    @Override
    @CacheFind(preKey = "securityQuestion")
    public List<AdminSecurity> showSecurityQuestion(Integer adminId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_id", adminId);
        List<AdminSecurity> securityList = adminSecurityMapper.selectList(queryWrapper);
        return securityList;
    }

    /**
     * @author Zcc
     * @desc 修改密码
     * @param   updatePassword,request
     * @return SysResult
     * @date 2020年11月21日20:33:54
     */
    @Override
    public SysResult updatePassword(UpdatePassword updatePassword,
                                    HttpServletRequest request) {
        QueryWrapper<AdminSecurity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("security_id", updatePassword.getSecurityId()).eq("security_answer", updatePassword.getSecurityAnswer());
        AdminSecurity adminSecurity1 = adminSecurityMapper.selectOne(queryWrapper);
        if (adminSecurity1!=null){
            Admin admin = (Admin) request.getSession().getAttribute("admin");
            SimpleHash sh = new SimpleHash(
                    "MD5",
                    updatePassword.getAdminPassword(),
                    admin.getSalt(),
                    10);
            admin.setAdminId(admin.getAdminId()).setAdminPassword(sh.toHex());
            adminMapper.updateById(admin);
            return SysResult.success();
        }
        return SysResult.fail();

    }

}
